package com.hzcx.app.simplechat.ui.moment.presenter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;

import com.hzcx.app.simplechat.api.BaseDialogObserver;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.base.BasePresenter;
import com.hzcx.app.simplechat.bean.UpLoadImgBean;
import com.hzcx.app.simplechat.model.MomentModel;
import com.hzcx.app.simplechat.model.PublicModel;
import com.hzcx.app.simplechat.model.SettingModel;
import com.hzcx.app.simplechat.ui.moment.contract.MomentCreateContract;
import com.hzcx.app.simplechat.ui.setting.contract.UpdatePhoneContract;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.file.FileUtils;
import com.hzcx.app.simplechat.util.token.TokenUtils;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MomentCreatePresenter extends BasePresenter<MomentCreateContract.View> implements MomentCreateContract.Presenter {


    private int upLoadIndex=0;
    private List<String> urlList=new ArrayList<>();

    /**
     *  上传图片
     * @param context
     * @param pathList
     */
    @Override
    public void upLoadImg(Activity context, List<String> pathList) {

        if(pathList==null||pathList.size()<=0){
            mView.upLoadSuccess("");
            return;
        }

        if(EmptyUtils.isEmpty(pathList.get(upLoadIndex))){
            uploadSuccess(context,pathList);
            return;
        }
        String url;
        File file=new File(pathList.get(upLoadIndex));
        if(file.exists()){
            url=file.getPath();
        }else {
            url= FileUtils.getImageAbsolutePath(context, Uri.parse(pathList.get(upLoadIndex)));
        }

        PublicModel.upLoadImg(context, TokenUtils.getTokenHeader(), url, new BaseObserver<UpLoadImgBean>() {
            @Override
            protected void onSuccess(UpLoadImgBean data) {
                urlList.add(data.getUrl());
                uploadSuccess(context,pathList);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                mView.hideLoading();
                upLoadIndex=0;
                urlList.clear();
            }
        });
    }

    /**
     * 图片或视频上传完成
     *
     * @param context
     * @param imgUrls
     */
    private void uploadSuccess(Activity context, List<String> imgUrls) {
        upLoadIndex++;
        if (upLoadIndex <= (imgUrls.size() - 1)) {
            upLoadImg(context, imgUrls);
        } else {
            String images = "";
            for (int i = 0; i < urlList.size(); i++) {
                if (i < (urlList.size() - 1)) {
                    images = images + urlList.get(i) + ",";
                } else {
                    images = images + urlList.get(i);
                }
            }
            upLoadIndex=0;
            urlList.clear();
            mView.upLoadSuccess(images);
        }
    }

    /**
     *  发布动态
     * @param context
     * @param maincontent
     * @param dy_images
     * @param visible
     * @param linkurl
     * @param subscript
     */
    @Override
    public void commitMoment(Context context, String maincontent, String dy_images, int visible, String linkurl, int subscript,
                             String lng,String lat,String address) {
        MomentModel.createMoment(context, maincontent, dy_images, visible, linkurl, subscript,lng,lat,address, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.hideLoading();
                mView.commitSuccess();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                mView.hideLoading();
            }
        });
    }
}
